;$(function(){
        	
            var op = addUsers();
            op.createTable();
            
            
            function addUsers(){
                var rs = {};
                
             	rs.table=function(msg){
             		$("table").empty()
                	var str1 ="<tr><td>编号</td><td>姓名</td><td>薪水</td><td>性别</td><td>生日</td></tr>"
                		$("table").append(str1);
                      msg.forEach(function(item,index){
                    	  var sex;
                    	if(item.sex){
                    		sex ="男";
                    	}else{
                    		sex = "女";
                    	}
                    	var date = new Date(item.birth);
                    	
                        var str2="<td>"
                        +item.id+"</td><td>"+item.name+"</td><td>"+item.sal+"</td><td>"+sex+"</td><td>"+date+"</td>"
                        
                        var btn1 = $("<button class='btn btn-default'>删除</button>").on('click',function(){
                        	rs.del(item.id);
                        });
                        var btn2 = $("<button type='button' class='btn btn-default' data-toggle='modal' data-target='#myModal'>更新</button>").on('click',function(){
                        	rs.update(item);
                        });
                        var tr = $("<tr></tr>").append(str2);
                        $("table").append(tr);
                        tr.append(btn1);
                        tr.append(btn2);
                        });
             	}
             	
             	
             	rs.del=function(id){
             		$.ajax({
                        type:"DELETE",
                        url:"http://localhost:8081/UserMVC/deleteUser?id="+id,
                    })
                        .done(function(msg){
                       	 rs.table(msg);
                    }); 
             	}
             	
             	rs.update=function(item){
             		 $("#id").val(item.id);
             		$("#update").on("click",function(){
                    	var user = $("#form2").serializeObject()
                    	console.log(user);
                    	//console.log()
                        $.ajax({
                            type:"POST",
                            url:"http://localhost:8081/UserMVC/updateUser",
                            data:JSON.stringify(user),
                            contentType:"application/json"
                        })
                        .done(function(msg){
                        	rs.table(msg);

                        
                        })
                        
                        
                    });
  
             	}
             	
                rs.createTable = function(){
                    $("#add").on("click",function(){
                    	var user = $("#form1").serializeObject()
                    	console.log(user);
                    	//console.log()
                        $.ajax({
                            type:"POST",
                            url:"http://localhost:8081/UserMVC/addUser",
                            data:JSON.stringify(user),
                            contentType:"application/json"
                        })
                        .done(function(msg){
                        	rs.table(msg);
                        
                        })
                        
                        
                    });
                    
                }
                
                return rs;
            } 
            
            
            $.fn.serializeObject = function()
        	{
        	    var o = {};
        	    var a = this.serializeArray();
        	    $.each(a, function() {
        	        if (o[this.name] !== undefined) {
        	            if (!o[this.name].push) {
        	                o[this.name] = [o[this.name]];
        	            }
        	            o[this.name].push(this.value || '');
        	        } else {
        	            o[this.name] = this.value || '';
        	        }
        	    });
        	    return o;
        	};
            
        }())